package com.example.xd.permissionlibrary

import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import com.example.xd.permissionlibrary.utils.toast
import com.example.xd.permissionlibrary.weigth.Permission_Dialog

/**
 * @auther XuDan on 2020/8/31.
 */
object FloatingPermission {
    fun isFloatingPermission(context: Context): Boolean {
        var isHas = false
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(context)) {
                isHas = true
            } else {
                isHas = false
            }
        } else {
            isHas = true
        }
        return isHas
    }

    fun getFloatingPermission(context: Context) {
        if (!isFloatingPermission(context)) {
            val permissionDialog = Permission_Dialog(context)
            permissionDialog.setContent("获取悬浮窗权限")
            permissionDialog.setDialogOkListener(object : Permission_Dialog.DialogOKListener {
                override fun dialogOk() {
                    val intent = Intent(context, SpecialPermissionActivity::class.java)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    intent.putExtra("type", "floating")
                    context.startActivity(intent)
                    permissionDialog.dismiss()
                }

                override fun dialogCancle() {
                    toast(context, "取消")
                    permissionDialog.dismiss()
                }

            })
            permissionDialog.show()
        }

    }
}